﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.Interfaces;
using AMS.DataAccess.BO;
using AMS.DataAccess.BO.Enums;

public partial class Customer_ucCustomerSummaryInfo : UCBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropdownLists();
        }
    }

    #region DataBind
    public void BindCustomer(int customerId)
    {
        ICustomerManager mgr = ManagerFactory.CreateCustomerManager();
        CustomerInfo customer = mgr.GetCustomer(customerId);
        if (customer != null)
        {
            txtAddress.Text = customer.Address;
            txtCustomerCode.Text = customer.IDCode;
            txtHomepage.Text = customer.HomePage;
            txtName.Text = customer.Name;
            txtName_En.Text = customer.Name_En;
            txtRepresenter.Text = customer.Represent;
            txtRepresenterPosition.Text = customer.RepresentPosition;
            txtZipCode.Text = customer.ZipCode;
            txtSpecialNeeds.Text = customer.SpecialNeeds;
            txtNote.Text = customer.Note;
            if (!string.IsNullOrEmpty(customer.CountryID))
            {
                drpCountry.SelectedValue = customer.CountryID;
            }

            if (!string.IsNullOrEmpty(customer.ProvinceId))
            {
                drpProvince.SelectedValue = customer.ProvinceId;
                BindCities(customer.ProvinceId);
                drpCity.SelectedValue = customer.CityId;
            }
        }
        else
        {
            // TODO: 
        }
    }

    void BindDropdownLists()
    {
        IProvinceCityManager pc = ManagerFactory.CreateProvinceManager();
        List<ProvinceCityInfo> provinces = pc.GetProvinces(CallContext);
        drpProvince.DataSource = provinces;
        drpProvince.DataTextField = "ProvinceName";
        drpProvince.DataValueField = "ProvinceID";
        drpProvince.DataBind();

        if (drpProvince.Items.Count > 0)
        {
            BindCities(drpProvince.Items[0].Value);
        }
    }

    void BindCities(string provinceId)
    {
        IProvinceCityManager mgr = ManagerFactory.CreateProvinceManager();
        List<ProvinceCityInfo> cities = mgr.GetCitiesByProvince(CallContext, provinceId);
        if (cities.Count == 0)
        {
            cities.Add(new ProvinceCityInfo(drpProvince.SelectedValue, drpProvince.Text));
        }

        drpCity.DataSource = cities;
        drpCity.DataTextField = ProvinceCityInfo.NameFieldName;
        drpCity.DataValueField = ProvinceCityInfo.IDFieldName;
        drpCity.DataBind();
    }
    #endregion

    /// <summary>
    /// 有些字段的默认值并没有填上
    /// </summary>
    /// <param name="customer"></param>
    /// <returns></returns>
    public bool GetCustomer(CustomerInfo customer)
    {
        if (!ValidateCustomer())
        {
            return false;
        }

        customer.Address = txtAddress.Text;
        customer.CityId = drpCity.SelectedValue;
        customer.CityName = drpCity.Text;
        customer.Note = txtNote.Text;
        customer.ProvinceId = drpProvince.SelectedValue;
        customer.ProvinceName = drpProvince.Text;
        customer.CountryID = drpCountry.SelectedValue;
        customer.Represent = txtRepresenter.Text;
        customer.RepresentPosition = txtRepresenterPosition.Text;
        customer.SpecialNeeds = txtSpecialNeeds.Text;
        customer.Name = txtName.Text;
        customer.Name_En = txtName_En.Text;
        customer.HomePage = txtHomepage.Text;
        customer.IDCode = txtCustomerCode.Text;
        customer.ZipCode = txtZipCode.Text;
        customer.CustomerType = (CustomerType)drpCustomerType.SelectedIndex;
        return true;
    }

    bool ValidateCustomer()
    {
        // TODO: add validate code rere
        return true;
    }

    protected void drpCountry_DataBound(object sender, EventArgs e)
    {
        drpCountry.SelectedValue = Constants.ChinaCountryID;
    }
    protected void drpProvince_DataBound(object sender, EventArgs e)
    {
        if (drpProvince.Items.Count > 0)
        {
            BindCities(drpProvince.SelectedValue);
        }
    }
    protected void drpProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCities(drpProvince.SelectedValue);
    }
}
